﻿//登录验证
function Validation(type) {
    var temp = true;
    var name = document.getElementById("txtUserName").value;
    var pwd = document.getElementById("txtPassword").value;
    var code = document.getElementById("txtCode").value;
    if (name == "") {
        alert("用户名不能为空!");
        $("#txtUserName").focus();
        temp = false;
    } else if (pwd == "") {
        alert("密码不能为空!");
        $("#txtPassword").focus();
        temp = false;
    }
//    else if (code == "") {
//        alert("验证码不能为空!");
//        $("#txtCode").focus();
//        temp = false;
//    }
    return temp;
}
function LoadVCode(obj) {
    if (obj == 0) {
        alert('验证码错误!');
        document.getElementById("txtCode").value = "";
        $("#txtCode").focus();
    } else if (obj == 1) {
        alert('用户名或密码错误!请重新登录!');
        document.getElementById("txtPassword").value = "";
        document.getElementById("txtCode").value = "";
        $("#txtPassword").focus();
    } else if (obj == 2) {
        alert("该用户未生效,禁止登录系统!");
        document.getElementById("txtUserName").value = "";
        document.getElementById("txtPassword").value = "";
        document.getElementById("txtCode").value = "";
        $("#txtUserName").focus();
    }
    document.getElementById("vimg").src = "VCode.aspx?time" + new Date().getTime();
}



///-------------------------------------
///供调用方法
///-------------------------------------
function Save() {
    if (checkInputData() == "0") {
        return;
    }
    if (typeof (trySave) == "function") {
        setValue();
        var json = $.toJSON(instance[0]).replace(/'/g, "[B]");
        //var json =$.toJSON(instance[0]).replace(/'/g,"[B]").replace(/\n/g,'[BR]');
        $("#jsonDataContent").val(json);
        trySave();
        return;
    }
    if (typeof (fnCustomSave) == "function") {
        fnCustomSave();
        return;
    }
    fnSystemSave();
}



function fnSystemSave() {
    ///初始化相关参数
    setValue();
    $("#jsonDataContent").val($.toJSON(instance[0]));
    var SaveUrl = url + "?r=" + Math.random() + "&entity=" + entity + "&action=" + action + "&json=" + encodeURI($("#jsonDataContent").val().replace(/&/g, "[A]").replace(/'/g, "[B]"));
    if (typeof (checkData) == "function") {
        if (checkData() == "0") {
            return;
        }
    }

    ///调用验证
    if (typeof (fnCheckData) == "function") {
        var msg = fnCheckData(instance[0]);
        if (!msg == "") {
            alert(msg);
            return;
        }
    }
    //发送保存请求
    YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
        success: responseSuccess
                ,
        failure: responseFailure
    }, null);
}



var formReForm;
function doReform() {
    // load the css
    formReForm = new FormReForm('fieldForm');
    //formReForm.loadformReFormCss();

    // do the reform with default settings
    formReForm.doReForm();

}


var trySave = function () {
    var formObject = document.getElementById('form2');
    YAHOO.util.Connect.setForm(formObject);
    var cObj = YAHOO.util.Connect.asyncRequest('POST', url + "?flag=1&action=" + action + "&entity=" + entity, callback);
}


var responseSuccess = function (res) {
    try {

        var str = res.responseText;
        if (str == "1") {
            alert("数据保存成功");
            if (action == 'insert') {
                //parent.fnAddRow(instance[0]);
                parent.ReLoadData();
            } else {
                parent.ReLoadData();
                //parent.fnUpdateRow(instance[0]);
            }
            parent.closeWin();
        }
        else {
            showMessage();
            alert(str + "\n" + "数据保存失败!");
        }
    }
    catch (e) {
    }
    finally {
    }
};

var responseFailure = function (error) {
    alert("数据错误：" + error.statusText);
}
var callback = {
    success: responseSuccess,
    failure: responseFailure
};

var SetCountrolValue = function (o) {
    $.each(o, function (property, value) {
        //try {
        value = value + "";
        // find the control and set value
        if (property != "") {
            var control = $("[name='" + property + "']");
            var strValue = value.replace(/@1@1@1/g, "'");
            strValue = strValue.replace(/@2@2@2/g, '"');
            strValue = strValue.replace(/@@@@/g, "\n");
            strValue = strValue.replace(/[BR]/g, "\n");
            control.val(strValue);
            try {
                $('#' + property).combobox('setValue', strValue);
            } catch (Error) { }
        }
    });
}